package com.lvmama.rhino.utils;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;


public class HttpSignUtil {
	private static final String key = "Vkd0U1UxSkdWbFZYYldoYVZtMDRlRmt5TVhOTmF6VllVbXhTYVdGc1dtRldhazVPVFZkSmVsSnVUazlXYlhoM1ZUTndWazFYU2xWWmVrWlBWMFZhUjFSclVsTlNSazQyVm14d2JGWnRZM2hXUldoSFpXczFWVlp1YUZOV1JscG9Wa1JLTTAxV2JGZGFSV3hQWWtkNFNWVXdWVFZOVmxZMlZtNWFWVTFyV2tOYVJrNHdWa1UxV1ZGcVJtaFhSbG8yVmtSR2EyRXlSWGRQVmxKUFVqSjNlRlpZY0dGaU1sSllVbXR3YUUxc1duTlhWbVJ2VlVkR1ZXSklaR3RXVkd4RFdWWk9NR0ZHVFhwUmJYaHBWbFZ3ZVZONlJrNU5SMUpaVm01Q1dHSlVSWGhXYWtKTFlqQnplVkpzYUdwTlJHd3dWakJvVjA1V1NraGpSelZVWVRKb01GbHJhRXRTVjBwSVkwWndUMDFxVlhsV01uUnZUVzFTY21OSVdrNVNSbG96V2xaV2MxUnNXa2hqU0ZKcFVrWndXbGt3YUVOa01rcDBUVlJHVkdWVWJIcFpNRll3WWxkS1NHRkhhR3hpVkVaM1dURmFiMk15UmxaaVNHeHBZbFJHY0ZwSE1ERmtSMGw0Vlc1R1lVMUhlRFZaYTJSM1V6RndkR1JFUm1wWFNFSXhXV3hqTlZaWFNraGpla3BZVWpOb00xWXhaR0ZrTVc5NFlrY3hhMkpzY0V4Wk1qRXdUVlpzVmxWdVVtRk5TR2Q1V1ZST1YyRkdiSFJQV0d4cVlURktlbGx0ZUhkU1YwVjZWbXR3YW1KWVVYaFpha3BMWkVkR1ZtTkZiR2xpVkVZeVZtdGpOVTFzYkZoVGJrWmhUVWQzZVZsVVRsZFRiRXBJVFZoT2FVMXNiM2xhUjNSelRtMUtkVlpVUWsxTmFrWXlWa1ZrZDJNeVRuVlJWRTVyWW14d1MxcFhNSGhsVm14WFZXNVNhRmRGV2xwWk1HUnJXVlpXU0dSRVJsVlRSWEF5V1d4YWQyVlhTa2hhUm5CaFlsUkdlbGt5ZEZOa01rcEZXa1JLYUdKWFVrdFZNVkYzVUZFOVBRPT0=";
	public static Map<String,String> getMapParamsForHttpPost(Map<String, Object> paraMap) {
		String sign = getSignFromMap(paraMap);
		String lvtukey = DigestUtils.md5Hex(sign+key);
		
		Iterator<Map.Entry<String, Object>> entries = paraMap.entrySet().iterator();
		Map<String,String> postParaMap = new HashMap<String, String>();
		while (entries.hasNext()) {
			Map.Entry<String, Object> entry = entries.next();
			postParaMap.put(entry.getKey(), (String)entry.getValue());
		}
		postParaMap.put("lvtukey", lvtukey);
        
		return postParaMap;
	}
	
	public static String genLoginUrl(String RequestUrl,Map<String, Object> paraMap) {
		String sign = getSignFromMap(paraMap);
		String lvtukey = DigestUtils.md5Hex(sign+key);
		paraMap.put("lvtukey", lvtukey);
		RequestUrl += getSignFromMap(paraMap);
		return RequestUrl;
	}
	
	@SuppressWarnings("unchecked")
	public static String getSignFromMap(Map<String, Object> paraMap) {
		List<String> keys = new ArrayList<String>(paraMap.keySet());

	    Collections.sort(keys); //键值ASCII码递增排序
	
	    StringBuilder sb = new StringBuilder();
	    for (String key : keys) {
	        Object value = paraMap.get(key);
	        if (value == null) {
	            continue;
	        }
	        if (value.getClass().isArray()) {
	            for (int i = 0; i < Array.getLength(value); i++) {
	                String item = Array.get(value, i).toString();
	                sb.append(key).append('=').append(item).append('&');
	            }
	        } else if(value instanceof List) {
	            List<Object> items = (List<Object>) value;
	            for (Object item : items) {
	                sb.append(key).append('=').append(item.toString()).append('&');
	            }
	        } else {
	            String str = value.toString();
	            sb.append(key).append('=').append(str).append('&');
	        }
	    }
	    if (sb.length() > 0) {
	        sb.deleteCharAt(sb.length() - 1);
	    }
	    return sb.toString();
    }

}
